package com.redxun.config;

import com.fasterxml.classmate.TypeResolver;
import com.google.common.collect.Sets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import springfox.documentation.builders.OperationBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiDescription;
import springfox.documentation.service.ResponseMessage;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.ApiListingScannerPlugin;
import springfox.documentation.spi.service.contexts.DocumentationContext;
import springfox.documentation.spring.web.readers.operation.CachingOperationNameGenerator;

/**
 * SwaggerAddtion.
 * @author aj
 */
@Component
public class SwaggerAddtion implements ApiListingScannerPlugin {
    @Override
    public List<ApiDescription> apply(DocumentationContext documentationContext) {
        Set<ResponseMessage> responseMessages = new HashSet<>();
        return new ArrayList<ApiDescription>(
                Arrays.asList(
                        new ApiDescription(
                                //url
                                "/oauth/token",
                                //描述
                                "UserToken",
                                Arrays.asList(
                                        new OperationBuilder(
                                                new CachingOperationNameGenerator())
                                                //http请求类型
                                                .method(HttpMethod.POST)
                                                .produces(Sets.newHashSet(MediaType.APPLICATION_JSON_VALUE))
                                                .summary("获取token")
                                                //方法描述
                                                .notes("获取token, headers 中需设置Authorization, 值为 Basic d2ViQXBwOndlYkFwcA==")
                                                //归类标签
                                                .tags(Sets.newHashSet("获取授权token"))
                                                .parameters(
                                                        Arrays.asList(
                                                                new ParameterBuilder()
                                                                        //参数描述
                                                                        .description("oauth2鉴权方式，默认为password")
                                                                        //参数数据类型
                                                                        .type(new TypeResolver().resolve(String.class))
                                                                        //参数名称
                                                                        .name("grant_type")
                                                                        //参数默认值
                                                                        .defaultValue("password")
                                                                        //参数类型
                                                                        .parameterType("query")
                                                                        .parameterAccess("access")
                                                                        //是否必填
                                                                        .required(true)
                                                                        //参数数据类型
                                                                        .modelRef(new ModelRef("string"))
                                                                        .build(),
                                                                new ParameterBuilder()
                                                                        .description("用户名")
                                                                        .type(new TypeResolver().resolve(String.class))
                                                                        .name("username")
                                                                        .parameterType("query")
                                                                        .parameterAccess("access")
                                                                        .required(true)
                                                                        .modelRef(new ModelRef("string"))
                                                                        .build(),
                                                                new ParameterBuilder()
                                                                        .description("密码")
                                                                        .type(new TypeResolver().resolve(String.class))
                                                                        .name("password")
                                                                        .parameterType("query")
                                                                        .parameterAccess("access")
                                                                        .required(true)
                                                                        .modelRef(new ModelRef("string"))
                                                                        .build()
                                                        ))
                                                .build()),
                                false)));
    }

    @Override
    public boolean supports(DocumentationType documentationType) {
        return DocumentationType.SWAGGER_2.equals(documentationType);
    }
}
